import consumer.Consumer;
import producer.StringProducer;
import user.list.SynchronizedBuffer;


public class Application {

	public static void main(String[] args) throws Exception {
		StringProducer[] producers = new StringProducer[Integer.parseInt(args[0])];
		@SuppressWarnings("unchecked")
		Consumer<String>[] consumers = new Consumer[Integer.parseInt(args[1])];
		SynchronizedBuffer<String> buffer = new SynchronizedBuffer<String>();
		int timeBetweenProductions = Integer.parseInt(args[2]);
		int timeBetweenConsumptions = Integer.parseInt(args[3]);
		int preProductionTime = Integer.parseInt(args[4]);
		int runDuration = Integer.parseInt(args[5]);

		for (int i = 0; i < producers.length; i++) {
			producers[i] = new StringProducer(i, buffer, timeBetweenProductions);
		}
		for (int i = 0; i < consumers.length; i++) {
			consumers[i] = new Consumer<String>(i, buffer, timeBetweenConsumptions);
		}

		for (Thread t : producers) {
			t.start();
		}
		Thread.sleep(preProductionTime);
		for (Thread t : consumers) {
			t.start();
		}

		Thread.sleep(runDuration);
		for (Thread t : producers) {
			t.interrupt();
		}
		for (Thread t : consumers) {
			t.interrupt();
		}

		for (Thread t : producers) {
			t.join();
		}
		for (Thread t : consumers) {
			t.join();
		}
	}

}
